﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using iLangl.Model;

namespace iLangl.BusinessModel.DAL.XMLStorage
{
	public class ContactDAL : BaseStorageDAL, IContactDAL
	{
		#region IContactDAL Members

		public void CreateContact(Contact contact, IDbTransaction transaction)
		{
			dtoXmlStorage.ContactRow contactRow = Storage.Contact.FindById(contact.Id);

			bool isNew = (contactRow == null);
			bool isChanged = false;
			if (isNew)
			{
				contactRow = Storage.Contact.NewContactRow();
				isChanged = true;
			}



			if (!isChanged)
			{

				isChanged |= !contact.Id.Equals(contactRow.Id);
				if ((!contactRow.IsEMailNull()) && (!String.IsNullOrEmpty(contact.Email)))
					isChanged |= !contact.Email.Equals(contactRow.EMail);
				if ((!contactRow.IsFirstNameNull())&&(!String.IsNullOrEmpty(contact.FirstName)))
					isChanged |= !contact.FirstName.Equals(contactRow.FirstName);
				if ((!contactRow.IsLastNameNull()) && (!String.IsNullOrEmpty(contact.LastName)))
					isChanged |= !contact.LastName.Equals(contactRow.LastName);
				if ((!contactRow.IsPhoneNull()) && (!String.IsNullOrEmpty(contact.Phone)))
					isChanged |= !contact.Phone.Equals(contactRow.Phone);
				if ((!contactRow.IsTitleNull()) && (!String.IsNullOrEmpty(contact.Title)))
					isChanged |= !contact.Title.Equals(contactRow.Title);
				if ((!contactRow.IsCustomerIdentityNull()) && (!String.IsNullOrEmpty(contact.CustomerIdentity)))
					isChanged |= !contact.CustomerIdentity.Equals(contactRow.CustomerIdentity);
				if ((!contactRow.IsTPIdentityNull()) && (!String.IsNullOrEmpty(contact.TPIdentity)))
					isChanged |= !contact.TPIdentity.Equals(contactRow.TPIdentity);

				if (contact.Company != null)
				{
					isChanged |= !contact.Company.Id.Equals(contactRow.CompanyId);
				}
			}

			contactRow.Id = contact.Id;

			if (contact.Company != null)
			{
				contactRow.CompanyId = contact.Company.Id;
			}

			if (!String.IsNullOrEmpty(contact.Email))
				contactRow.EMail = contact.Email;

			if (!String.IsNullOrEmpty(contact.FirstName))
				contactRow.FirstName = contact.FirstName;

			if (!String.IsNullOrEmpty(contact.LastName))
				contactRow.LastName = contact.LastName;

			if (!String.IsNullOrEmpty(contact.Phone))
				contactRow.Phone = contact.Phone;

			if (!String.IsNullOrEmpty(contact.Title))
				contactRow.Title = contact.Title;

			if (!String.IsNullOrEmpty(contact.CustomerIdentity))
				contactRow.CustomerIdentity = contact.CustomerIdentity;

			if (!String.IsNullOrEmpty(contact.TPIdentity))
				contactRow.TPIdentity = contact.TPIdentity;


			if (isNew)
			{
				Storage.Contact.AddContactRow(contactRow);
			}

			if (isChanged)
			{
				contactRow.TimeSpan = CurrentTimeSpan;
			}

			if (transaction == null)
			{
				AcceptChanges();
			}
		}

		public void UpdateContact(Contact contact, IDbTransaction transaction)
		{
			CreateContact(contact, transaction);
		}


		public Contact ReadContact(string contactId)
		{
			dtoXmlStorage.ContactRow contactRow = Storage.Contact.FindById(contactId);

			if (contactRow == null)
				return null;

			Contact contact = new Contact();

			contact.Id = contactRow.Id;

			if (!contactRow.IsEMailNull())
				contact.Email = contactRow.EMail;

			if (!contactRow.IsFirstNameNull())
				contact.FirstName = contactRow.FirstName;


			if (!contactRow.IsLastNameNull())
				contact.LastName = contactRow.LastName;

			if (!contactRow.IsPhoneNull())
				contact.Phone = contactRow.Phone;

			if (!contactRow.IsTitleNull())
				contact.Title = contactRow.Title;


			if (!contactRow.IsCustomerIdentityNull())
				contact.CustomerIdentity = contactRow.CustomerIdentity;

			if (!contactRow.IsTPIdentityNull())
				contact.TPIdentity = contactRow.TPIdentity;

			contact.Company = DALFacade.Instance.CompanyDAL.ReadCompany(contactRow.CompanyId);

			return contact;

		}

		#endregion
	}
}